import java.util.ArrayList;
import java.util.List;

/*
    @author TTDB
    @create 2025/9/4
    @description: 回溯，组合总和
*/
public class combinationSum {
    public List<List<Integer>> combinationSum(int[] candidates, int target)
    {
        List<List<Integer>> res = new ArrayList<>();
        dfs(candidates, target, 0, new ArrayList<>(), res);
        return res;
    }
    private void dfs(int[] candidates, int target, int index, List<Integer> path, List<List<Integer>> res){
        if(target < 0) return;
        if(target == 0) {
            res.add(new ArrayList<>(path));
            return;
        }
        for(int i = index; i < candidates.length; i++) {
            path.add(candidates[i]);
            dfs(candidates, target - candidates[i], i, path, res);
            path.remove(path.size() - 1);
        }
    }
}
